/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.settlement.item;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.settlement.item.SettlementItem;
import com.server.modules.sys.service.settlement.item.SettlementItemService;

/**
 * 结算明细Controller
 * @author 丁光彪
 * @version 2016-11-21
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/settlement.item/settlementItem")
public class SettlementItemController extends BaseController {

	@Autowired
	private SettlementItemService settlementItemService;
	
	@ModelAttribute
	public SettlementItem get(@RequestParam(required=false) String id) {
		SettlementItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = settlementItemService.get(id);
		}
		if (entity == null){
			entity = new SettlementItem();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:settlement.item:settlementItem:view")
	@RequestMapping(value = {"list", ""})
	public String list(SettlementItem settlementItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SettlementItem> page = settlementItemService.findPage(new Page<SettlementItem>(request, response), settlementItem); 
		model.addAttribute("page", page);
		model.addAttribute("settlementItem", settlementItem);
		return "modules/sys/settlement.item/settlementItemList";
	}

	@RequiresPermissions("sys:settlement.item:settlementItem:view")
	@RequestMapping(value = "form")
	public String form(SettlementItem settlementItem, Model model) {
		model.addAttribute("settlementItem", settlementItem);
		return "modules/sys/settlement.item/settlementItemForm";
	}

	@RequiresPermissions("sys:settlement.item:settlementItem:edit")
	@RequestMapping(value = "save")
	public String save(SettlementItem settlementItem, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, settlementItem)){
			return form(settlementItem, model);
		}
		settlementItemService.save(settlementItem);
		addMessage(redirectAttributes, "保存结算明细成功");
		return "redirect:"+Global.getAdminPath()+"/sys/settlement.item/settlementItem/?repage";
	}
	
	@RequiresPermissions("sys:settlement.item:settlementItem:edit")
	@RequestMapping(value = "delete")
	public String delete(SettlementItem settlementItem, RedirectAttributes redirectAttributes) {
		settlementItemService.delete(settlementItem);
		addMessage(redirectAttributes, "删除结算明细成功");
		return "redirect:"+Global.getAdminPath()+"/sys/settlement.item/settlementItem/?repage";
	}

}